#!/usr/bin/env bash

set -e

cargo build --release

build_wasm_args=
if [[ $1 == "--docker" ]]; then
  build_wasm_args="--docker"
  shift
fi

filter_grammar_name=$1

root_dir=$PWD
tree_sitter=${root_dir}/target/release/tree-sitter
grammars_dir=${root_dir}/test/fixtures/grammars
grammar_files=$(find $grammars_dir -name grammar.js | grep -v node_modules)

while read -r grammar_file; do
  grammar_dir=$(dirname "$grammar_file")
  grammar_name=$(basename "$grammar_dir")

  if [[ -n $filter_grammar_name && "$filter_grammar_name" != "$grammar_name" ]]; then
    continue
  fi

  echo "Compiling ${grammar_name} parser to wasm"
  "$tree_sitter" build-wasm $build_wasm_args $grammar_dir
done <<< "$grammar_files"

mv tree-sitter-*.wasm target/release/
